//
//  PrivateVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/11/3.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "PrivateVC.h"
#import "IGAddPhotoCell.h"
#import "IGGetUploadWorksApi.h"
#import "IGAddAtlasDetailApi.h"
#import "UploadWorkModel.h"

@interface PrivateVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (strong, nonatomic) IBOutlet UITableView *privateTable;
@property(nonatomic,strong)IGGetUploadWorksApi *upLoadWorkApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *pageNumber;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, copy) NSString *queryTime;
@property (nonatomic, copy) NSString *secrecy;
@property(nonatomic,strong)IGAddAtlasDetailApi *addAltasDetailApi;

@end

@implementation PrivateVC
#pragma mark -懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.privateTable.backgroundColor =  [UIColor c2Color];
    [self.privateTable registerClass:[IGAddPhotoCell class] forCellReuseIdentifier:@"addPhotoCell"];
    self.privateTable.delegate=self;
    self.privateTable.dataSource=self;
    self.privateTable.emptyDataSetSource = self;
    self.privateTable.emptyDataSetDelegate = self;
    _secrecy=@"2";
    _pageSize = @"10";
    _pageNumber = @"0";
    _queryTime = @"";
    self.upLoadWorkApi=[[IGGetUploadWorksApi alloc]initWithAid:self.aid secrecy:_secrecy PageNumber:_pageNumber pageSize:_pageSize queryTime:_queryTime];
    self.addAltasDetailApi=[IGAddAtlasDetailApi new];
    @KWeakObj(self)
    [self.privateTable ig_setHeaderWithRefreshingBlock:^{
        _secrecy=@"2";
        _pageSize = @"10";
        _pageNumber = @"0";
        _queryTime = @"";
        [selfWeak requestDate];
    }];
    [self requestDate];

}
-(void)requestDate{
    self.upLoadWorkApi.secrecy=_secrecy;
    self.upLoadWorkApi.pageSize=_pageSize;
    self.upLoadWorkApi.pageNumber=_pageNumber;
    self.upLoadWorkApi.queryTime=_queryTime;
    @KWeakObj(self)
    [self.upLoadWorkApi startWithDecodeClass:[IGGetUploadWorksResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error)
     {
         [self.privateTable ig_endHeaderRefreshing];
         [self.privateTable ig_endFooterRefreshing];
         if (responseObject) {
             IGGetUploadWorksResp *resp=responseObject;
             IGGetUploadWorksModel*model=resp.data;
             if (model.lstWork&&model.lstWork.count>0) {
                 [selfWeak setupFooter];
             }
             else
             {
                 [self.privateTable ig_endFooterRefreshingWithNoMoreData];
             }
             _queryTime = model.queryTime;
             if ([_pageNumber isEqualToString:@"0"]){
                 _dataArr = [NSMutableArray arrayWithArray:model.lstWork];
             }
             else
             {
                 [_dataArr addObjectsFromArray:model.lstWork];
             }
             [self.privateTable reloadData];
         }
         else
         {
             //空白页处理逻辑写在这里
         }
     }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.privateTable ig_setFooterWithRefreshingBlock:^{
        _pageNumber = [NSString stringWithFormat:@"%ld", [_pageNumber integerValue] + 1];
        [selfWeak requestDate];
    }];
}

- (void)scrollToTOP
{
    [self.privateTable setContentOffset:CGPointMake(0, 0) animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.privateTable ig_beginHeaderRefreshing];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count > 0 ? self.dataArr.count : 0;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGAddPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addPhotoCell"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (cell==nil)
    {
        cell = [[IGAddPhotoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"addPhotoCell"];
    }
    cell.data=_dataArr[indexPath.section];
    UploadWorkModel *workInfo=_dataArr[indexPath.section];
    cell.upInfo=workInfo;
    __weak typeof(self) weakSelf = self;
    cell.attenBlock = ^{
        weakSelf.addAltasDetailApi.aId = self.aid;
        weakSelf.addAltasDetailApi.pids= workInfo.pId;
        [weakSelf.addAltasDetailApi startWithDecodeClass:[IGAddAtlasDetailResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            if (responseObject) {
                workInfo.ifExist = [workInfo.ifExist boolValue]?@"0":@"1";
                [weakSelf.privateTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//                [weakSelf.privateTable ig_beginHeaderRefreshing];
            }
        }];
    };
    [cell setNeedsLayout];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return 10;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[NSAttributedString alloc] initWithString:@"您还没有上传任何作品~" attributes:nil];
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"No Upload", nil) attributes:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
